package com.yonyougov.devops.cruxcisupport.committest;

import com.yonyougov.devops.cruxcisupport.committest.dto.*;
import com.yonyougov.devops.cruxcisupport.committest.dto.release.ReleaseCommitTestDto;
import com.yonyougov.devops.cruxcisupport.committest.dto.release.ReleaseCommitTestRequestDto;
import com.yonyougov.devops.cruxcisupport.committest.vo.*;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.api.enums.BaseErrorCode;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Slf4j
@Api(tags = "提测模块")
@RestController
@RequestMapping("/api/v2/committest")
public class CommitTestController extends CrudController<CommitTestEntity, BigInteger> {

    private final CommitTestService commitTestService;

    @Override
    public BaseService<CommitTestEntity, BigInteger> getService() {
        return commitTestService;
    }

    public CommitTestController(CommitTestService commitTestService) {
        this.commitTestService = commitTestService;
    }

    //<!--用户自定义代码开始-->
    @ApiOperation(value = "提测或者重新提测调用", notes = "提测或者重新提测调用")
    @PostMapping(value = "/ct/add")
    @SneakyThrows
    public BaseResponseDto commitOrReCommit(@RequestBody @Valid CommitOrReCommitRequestDto request) {
        CommitOrReCommitDto commitOrReCommitDto = new CommitOrReCommitDto();
        BeanUtils.copyProperties(request, commitOrReCommitDto);

        BigInteger traceId = commitTestService.commitOrReCommit(commitOrReCommitDto);

        // 返回结果
        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("id", traceId);
        return result;
    }

    @ApiOperation(value = "前端开始测试调用", notes = "开始测试")
    @PostMapping(value = "/ct/start")
    @SneakyThrows
    public BaseResponseDto startCommitTest(@RequestBody @Valid InvalidCommitTestDto request) {
        commitTestService.startTest(request.getId());

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "提测驳回", notes = "提测驳回")
    @PostMapping(value = "/ct/reject")
    public BaseResponseDto rejectCommitTest(@RequestBody @Valid RejectCommitTestRequestDto request) {
        RejectCommitTestDto rejectCommitTestDto = new RejectCommitTestDto();
        BeanUtils.copyProperties(request, rejectCommitTestDto);

        boolean result = commitTestService.rejectCommitTest(rejectCommitTestDto);

        if (result) {
            return BaseResponseDto.getSuccessInstance();
        } else {
            return BaseResponseDto.getFailInstance(BaseErrorCode.SYS_ERROR);
        }
    }

    @ApiOperation(value = "提测通过", notes = "提测通过")
    @PostMapping(value = "/ct/pass")
    public BaseResponseDto passCommitTest(@RequestBody @Valid PassCommitTestRequestDto request) {
        PassCommitTestDto passCommitTestDto = new PassCommitTestDto();
        BeanUtils.copyProperties(request, passCommitTestDto);

        boolean result = commitTestService.passCommitTest(passCommitTestDto);

        if (result) {
            return BaseResponseDto.getSuccessInstance();
        } else {
            return BaseResponseDto.getFailInstance(BaseErrorCode.SYS_ERROR);
        }
    }

    //@ApiOperation(value = "前端发版调用", notes = "发版")
    //@PostMapping(value = "/ct/release")
    //@SneakyThrows
    //public BaseResponseDto releaseCommitTest(@RequestBody @Valid ReleaseCommitTestRequestDto request) {
    //    ReleaseCommitTestDto releaseCommitTestDto = new ReleaseCommitTestDto();
    //    BeanUtils.copyProperties(request, releaseCommitTestDto);
    //
    //    boolean result = commitTestService.releaseCommitTest(releaseCommitTestDto);
    //
    //    if (result) {
    //        return BaseResponseDto.getSuccessInstance();
    //    } else {
    //        return BaseResponseDto.getFailInstance(BaseErrorCode.SYS_ERROR);
    //    }
    //}

    @ApiOperation(value = "发版预览", notes = "发版预览")
    @PostMapping(value = "/ct/release/pre")
    @SneakyThrows
    public void preReleaseCommitTest(@RequestBody @Valid ReleaseCommitTestRequestDto request, HttpServletResponse response) {
        ReleaseCommitTestDto releaseCommitTestPlusDto = new ReleaseCommitTestDto();
        BeanUtils.copyProperties(request, releaseCommitTestPlusDto);

        response.setHeader("Context-Type", "application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + UUID.randomUUID().toString() + ".xls");

        commitTestService.preReleaseCommitTest(releaseCommitTestPlusDto, response.getOutputStream());
    }

    @ApiOperation(value = "前端发版调用", notes = "前端发版调用")
    @PostMapping(value = "/ct/releaseplus")
    @SneakyThrows
    public BaseResponseDto releaseCommitTest(@RequestBody @Valid ReleaseCommitTestRequestDto request) {
        ReleaseCommitTestDto releaseCommitTestPlusDto = new ReleaseCommitTestDto();
        BeanUtils.copyProperties(request, releaseCommitTestPlusDto);

        boolean result = commitTestService.releaseCommitTestPlus(releaseCommitTestPlusDto);

        if (result) {
            return BaseResponseDto.getSuccessInstance();
        } else {
            return BaseResponseDto.getFailInstance(BaseErrorCode.SYS_ERROR);
        }
    }

    @ApiOperation(value = "前端提测作废调用", notes = "提测作废")
    @PostMapping(value = "/ct/delete")
    @SneakyThrows
    public BaseResponseDto invalidOneCommitTest(@RequestBody @Valid InvalidCommitTestDto request) {
        commitTestService.invalidOneCommitTest(request.getId());

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "需求通过邮件进行功能确认时调用(还没搞完)", notes = "需求功能确认")
    @GetMapping(value = "/ct/confirm")
    @SneakyThrows
    public BaseResponseDto FuncConfirm(CommitTestFuncConfireRequestDto request) {
        List<String> attachmentUrls = new ArrayList<>();
        String[] split = request.getAttachmentUrls().split(",");
        if (split.length > 0) {
            attachmentUrls.addAll(Arrays.stream(split).collect(Collectors.toList()));
        }
        commitTestService.funcConfirm(request.getId(), attachmentUrls);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "更改提测单信息(根据当前用户权限)", notes = "更改提测单信息(根据当前用户权限)")
    @PostMapping(value = "/ct/upd")
    @SneakyThrows
    public BaseResponseDto upd(@RequestBody @Valid UpdateCommitTestByCurrentUserPermissionRequestDto request) {
        UpdateCommitTestByCurrentUserPermissionDto serviceDto = UpdateCommitTestByCurrentUserPermissionDto.builder().build();
        BeanUtils.copyProperties(request, serviceDto);

        commitTestService.updateCommitTestByCurrentUserPermission(serviceDto);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端提测列表调用", notes = "分页模糊查询开始提测列表")
    @PostMapping(value = "/ct/cs/page/like")
    public BaseResponseDto query(@RequestBody @Valid QueryListRequestDto request) {
        QueryListDto queryListDto = new QueryListDto();
        BeanUtils.copyProperties(request, queryListDto);

        Pagination<QueryCommitTestVo> pagination = commitTestService.query(queryListDto);

        BaseResponseDto responseDto = BaseResponseDto.getSuccessInstance();
        responseDto.put("totalCount", pagination.getTotalCount());
        responseDto.put("data", pagination.getDatas());
        return responseDto;
    }

    @SneakyThrows
    @ApiOperation(value = "唯一确定一条提测单", notes = "根据提测id或者提测code,优先走提测id")
    @PostMapping(value = "/ct/query/one")
    public BaseResponseDto query(@RequestBody @Valid QueryRequestDto request) {
        QueryDto queryDto = new QueryDto();
        BeanUtils.copyProperties(request, queryDto);

        QueryCommitTestVo queryCommitTestVo = commitTestService.query(queryDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", queryCommitTestVo);
        return result;
    }

    @ApiOperation(value = "统计-提测", notes = "统计-提测")
    @PostMapping(value = "/ct/sc/page/like")
    public BaseResponseDto statisticsCommitTest(@RequestBody @Valid StatisticsCommitTestRequestDto request) {
        StatisticsCommitTestDto statisticsCommitTestDto = new StatisticsCommitTestDto();
        BeanUtils.copyProperties(request, statisticsCommitTestDto);

        Pagination<StatisticsCommitTestVo> pagination = commitTestService.statisticsCommitTest(statisticsCommitTestDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }

    @ApiOperation(value = "统计-驳回", notes = "统计-驳回")
    @PostMapping(value = "/ct/sr/page/like")
    public BaseResponseDto statisticsRejected(@RequestBody @Valid StatisticsRejectedRequestDto request) {
        StatisticsRejectedDto statisticsRejectedDto = new StatisticsRejectedDto();
        BeanUtils.copyProperties(request, statisticsRejectedDto);

        Pagination<StatisticsRejectedVo> pagination = commitTestService.statisticsRejected(statisticsRejectedDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }

    @ApiOperation(value = "统计-驳回耗时", notes = "统计-驳回耗时")
    @PostMapping(value = "/ct/sr/time/page/like")
    public BaseResponseDto statisticsRejectedConsumeTime(@RequestBody @Valid StatisticsRejectedConsumeTimeRequestDto request) {
        StatisticsRejectedConsumeTimeDto statisticsRejectedConsumeTimeDto = new StatisticsRejectedConsumeTimeDto();
        BeanUtils.copyProperties(request, statisticsRejectedConsumeTimeDto);

        List<StatisticsRejectedConsumeTimeVo> datas = commitTestService.statisticsRejectedConsumeTime(statisticsRejectedConsumeTimeDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", datas);
        return result;
    }
    //<!--用户自定义代码结束-->
}
